﻿using System.IO;
using ComponentSystem;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace LubanSystem
{
    /// <summary>
    /// Resources路径支持组件
    /// </summary>
    public partial class Resources_Com : ComponentWithId
    {
        
    }
    
    public partial class Resources_Com
    {
        /// <summary>
        /// 检查资源是否存在
        /// </summary>
        /// <param name="assetPath">资源路径</param>
        /// <returns>是否存在</returns>
        public bool Exist(string assetPath)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogError($"LubanSystem Resources_Com assetPath 为空");
                return false;
            }

            // 移除可能的文件扩展名
            string pathWithoutExtension = Path.ChangeExtension(assetPath, null);
            // 检查资源是否存在
            bool isExist = Resources.Load<TextAsset>(pathWithoutExtension) != null;
            
            return isExist;
        }

        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <returns>配置文件内容</returns>
        public string LoadConfigs(string path)
        {
            if (!this.Exist(path)) return string.Empty;
            
            string pathWithoutExtension = Path.ChangeExtension(path, null);
            TextAsset textAsset = Resources.Load<TextAsset>(pathWithoutExtension);
            if (textAsset == null)
            {
                Debug.LogError($"LubanSystem Resources_Com 加载资源失败: {path}");
                return string.Empty;
            }
            
            return textAsset.text;
        }
    }
}